﻿using Direct.IDAL.Manager.UserCenter;
using DirectSell.Model.TreeList;
using DirectSell.Model.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectSell.DAL.Manager.UserCenter
{
    public class BackRefreeMapRepository:BaseManagerRepository, IBackRefreeMapRepository
    {
        #region 查询第一层
        /// <summary>
        /// 查询第一层
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public virtual async Task<UserInforMationDetails> GetInfo(string UserCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT UserId,UserName,UserCode,LevelName,UserStatus  ");
            sb.Append(" FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserCode='{0}' ", UserCode);
            var result = await base.QueryAsync<UserInforMationDetails>(sb.ToString());
            return result.FirstOrDefault();
        }
        #endregion

        #region 根据推荐人向后查
        /// <summary>
        /// 根据推荐人向后查
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public async Task<IList<UserInforMationDetails>> QueryBehind(string UserCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT UserId,UserName,UserCode,LevelName,UserStatus  ");
            sb.Append(" FROM UserInforMations ");
            sb.AppendFormat(" WHERE RefereeCode='{0}' ", UserCode);
            var result = await base.QueryAsync<UserInforMationDetails>(sb.ToString());
            return result as IList<UserInforMationDetails>;
        }
        #endregion

        #region 根据ID获取编号
        /// <summary>
        ///根据ID获取编号
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public string QueryCode(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT UserCode  ");
            sb.Append(" FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserId='{0}' ", id);
            return base.QueryFirstT<string>(sb.ToString());
        }
        #endregion

        #region 判断是否为父级
        /// <summary>
        /// 判断是否为父级
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public bool CheckIsParent(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT count(*) FROM ");
            sb.AppendFormat(" UserInforMations WHERE RefereeCode='{0}' ", usercode);
            return base.QueryFirstT<int>(sb.ToString()) > 0;
        }
        #endregion

        #region 获取树状图
        /// <summary>
        /// 获取树状图
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public async Task<IList<ZTreeList>> GetZtreeList(int id)
        {
            var UserCode = QueryCode(id);
            bool isparent = CheckIsParent(UserCode);
            IList<ZTreeList> list = new List<ZTreeList>();
            var firstList = await GetInfo(UserCode);
            list.Add(new ZTreeList()
            {
                id = firstList.UserId,
                name = firstList.UserName,
                pid = 0,
                isParent = CheckIsParent(firstList.UserCode)
            });


            return list;
        }
        #endregion

        #region 查询下一级
        /// <summary>
        /// 查询下一级
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public async Task<IList<ZTreeList>> GetNext(int userid)
        {
            var UserCode = QueryCode(userid);
            IList<ZTreeList> list = new List<ZTreeList>();
            var pinfo = await GetInfo(UserCode);
            var nextList = await QueryBehind(UserCode);
            if (nextList.Count > 0)
            {
                foreach (var item in nextList)
                {
                    list.Add(new ZTreeList()
                    {
                        id = item.UserId,
                        name = item.UserName,
                        pid = pinfo.UserId,
                        isParent = CheckIsParent(item.UserCode)
                    });
                }
            }
            return list;
        }
        #endregion
    }
}
